<?php

namespace app\health\controller;

use app\health\validate\SmsValidate;
use app\health\library\Sms as Smslib;
use Exception;
use Overtrue\EasySms\EasySms;
use think\Cache;

/**
 * 短信发送
 * @Author 张旭鹏
 * @DateTime 2023-07-04
 */
class Sms extends ApiBase
{
    protected $noNeedLogin = ['send'];

    /**
     * 发送手机验证码
     * @return void
     * @throws Exception
     * @Author 张旭鹏
     * @DateTime 2023-07-04
     */
    public function send(): void
    {
        try {
            // 短信验证码场景
            $scene   = $this->request->param('scene', 'login');
            $params  = (new SmsValidate())->goCheck($scene, ['scene' => $scene]);
            $code    = random_int(1000, 9999);
            $easySms = new EasySms(config('easy_sms'));
            $easySms->send($params['mobile'], ['data' => ['code' => $code], 'template' => 'SMS_285020544']);
        } catch (Exception $exception) {
            $this->error(__('发送失败'));
        }
        Cache::rm("sendSmsCodeVerifyNum:" . $scene . ":" . $params['mobile']);
        Cache::set("sendSmsCode:" . $scene . ":" . $params['mobile'], $code, 120);
        $this->success(__('发送成功'));
    }
}
